﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;

using ZhangBY.Config;

namespace ZhangBY.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        /// <summary>
        /// 获取分类表（conclass）记录
        /// </summary>
        /// <returns></returns>
        public DataSet GetConClass()
        {
            return DbHelper.ExecuteDataset(CommandType.Text, string.Format("SELECT * FROM [dbo].[{0}conclass]", BaseConfigs.GetTablePrefix));
        }

        /// <summary>
        /// 获取分类当前节点和所有父节点记录
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllClassParentInfo(int id)
        {
            DbParameter getParms=DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,id);
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, "[dbo].[sp_getAllClsParent]", getParms);
        }

        /// <summary>
        /// 更新CONCLASS记录
        /// </summary>
        /// <param name="id">id</param>
        /// <param name="pid">父ID</param>
        /// <param name="classname">分类名称</param>
        /// <param name="classkey">关键字</param>
        /// <param name="classdescp">描述</param>
        /// <returns></returns>
        public int UpdateConClass(int id, int pid, string classname, string classkey, string classdescp)
        {
            DbParameter[] getParms ={
                                       DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,id),
                                       DbHelper.MakeInParam("@classpid",(DbType)SqlDbType.Int,int.MaxValue,pid),
                                       DbHelper.MakeInParam("@classname",(DbType)SqlDbType.NChar,20,classname),
                                       DbHelper.MakeInParam("@classkey",(DbType)SqlDbType.NChar,40,classkey),
                                       DbHelper.MakeInParam("@classdescp",(DbType)SqlDbType.NChar,40,classdescp),
                                       DbHelper.MakeOutParam("@outnum",(DbType)SqlDbType.Int,int.MaxValue)
                                    };
            int iRows = DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "[dbo].[sp_updateconclass]", getParms);
            return Convert.ToInt32(getParms[getParms.Length - 1].Value);
        }
        /// <summary>
        /// 更新子节点数
        /// isADD为真是增加子节点反之减少子节点
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isAdd">是否增加</param>
        /// <returns></returns>
        public int UpdateClassChildCount(int id,bool isAdd)
        {
            DbParameter getParms = DbHelper.MakeInParam("@id",(DbType)SqlDbType.Int,int.MaxValue,id);
            string commandText = string.Format("UPDATE [dbo].[{0}conclass] SET [classchildcount]={1} WHERE [id]=@id", BaseConfigs.GetTablePrefix, isAdd ? "[classchildcount]+1" : "[classchildcount]-1");
            return DbHelper.ExecuteNonQuery(CommandType.Text,commandText,getParms);
        }
        /// <summary>
        /// 向CONCLASS表插入一条新分类
        /// </summary>
        /// <param name="pid">父ID</param>
        /// <param name="classname">分类名</param>
        /// <param name="classkey">分类关键字</param>
        /// <param name="classdescp">分类描述</param>
        /// <returns></returns>
        public int InsertConClass(int pid,string classname,string classkey,string classdescp)
        {
            DbParameter[] getParms = {
                                         DbHelper.MakeInParam("@classpid",(DbType)SqlDbType.Int,int.MaxValue,pid),
                                         DbHelper.MakeInParam("@classname",(DbType)SqlDbType.NChar,20,classname),
                                         DbHelper.MakeInParam("@classkey",(DbType)SqlDbType.NChar,40,classkey),
                                         DbHelper.MakeInParam("@classdescp",(DbType)SqlDbType.NChar,40,classdescp)
                                     };
            string commandText = string.Format("insert into[dbo].[{0}conclass] ([classname],[classpid],[classkey],[classchildcount],[classleaf],[classpath],[classdescp])values(@classname,@classpid,@classkey,0,0,0,@classdescp)",BaseConfigs.GetTablePrefix);
            return DbHelper.ExecuteNonQuery(CommandType.Text,commandText,getParms);
        }


    }
}
